package com.bw.mallshop.mvvm.viewmodel;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.R;
import com.bw.mallshop.adapter.ShipeAddressAdapter;
import com.bw.mallshop.dao.UserSingle;
import com.bw.mallshop.entity.DelAddressEntity;
import com.bw.mallshop.entity.ShipAddress;
import com.bw.mallshop.entity.ShipAddressEntity;
import com.bw.mallshop.field.EventAction;
import com.bw.mallshop.mvvm.model.ShipAddressModel;
import com.bw.mallshop.mvvm.view.ShipEdtAddressActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ShipAddressListViewModel extends BaseViewModel<ShipAddressModel>implements
        OnItemClickListener, OnItemChildClickListener {

    public ShipeAddressAdapter adapter;
    public int type = 0;

    public void finish(){
        ShipAddress shipAddress = new ShipAddress();
        shipAddress = adapter.getData().get(0);
        for (ShipAddress entity:adapter.getData()){
            if (entity.ship_is_default==0){
                shipAddress = entity;
                break;
            }
        }
        finishPage();
        EventAction action = new EventAction();
        action.action = "sel_ship";
        action.obj = shipAddress;
        EventBus.getDefault().post(action);
    }

    public void createAddress(){
        startPage(ShipEdtAddressActivity.class);
    }

    @Override
    protected void initData() {
        adapter = new ShipeAddressAdapter();
        adapter.setOnItemClickListener(this);
        adapter.setOnItemChildClickListener(this);
        Map<String,Object>map=new HashMap<>();
        map.put("id", UserSingle.getInstance().userEntityValues.id);
        m.requestAddressList(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof ShipAddressEntity){
            ShipAddressEntity shipAddressEntity = (ShipAddressEntity) entity;
            adapter.setNewInstance(shipAddressEntity.data);
        }else if (entity instanceof DelAddressEntity){
            //删除完成，刷新数据
            Map<String,Object>map=new HashMap<>();
            map.put("id", UserSingle.getInstance().userEntityValues.id);
            m.requestAddressList(map).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }
    }

    public void refreshShip(){
        Map<String,Object>map=new HashMap<>();
        map.put("id", UserSingle.getInstance().userEntityValues.id);
        m.requestAddressList(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("ship",this.adapter.getItem(position));
        startPage(ShipEdtAddressActivity.class,bundle);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        if (R.id.mDeleteTv == view.getId()){
            //删除地址
            Map<String,Object>map=new HashMap<>();
            map.put("id",this.adapter.getItem(position).id);
            map.put("userId",this.adapter.getItem(position).user_id);
            m.requestDelAddress(map).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }else {
            //编辑地址
            Bundle bundle = new Bundle();
            bundle.putSerializable("ship",this.adapter.getItem(position));
            startPage(ShipEdtAddressActivity.class,bundle);
        }

    }
}
